//
//  ZYExtension.swift
//  SmartCharging
//
//  Created by andylau on 16/6/7.
//  Copyright © 2016年 sherry. All rights reserved.
//

import Foundation
import MBProgressHUD

@IBDesignable
class RoundedCornersView: UIView {
    @IBInspectable var cornerRadius: CGFloat = 0 {
        didSet {
            layer.cornerRadius = cornerRadius
            layer.masksToBounds = cornerRadius > 0
        }
    }

    @IBInspectable var borderWidth: CGFloat = 0 {
        didSet {
            layer.borderWidth = borderWidth
        }
    }
    @IBInspectable var borderColor: UIColor? {
        didSet {
            layer.borderColor = borderColor?.cgColor
        }
    }

}

extension UIView {
    // 可给全局继承自UIView的元素切圆角
    func cuttingCorner(radian: CGFloat) {
        layer.cornerRadius = radian
        layer.masksToBounds = true
    }

    // 可给全局继承自UIButton的元素切给定border值
    func setupBorder(width: CGFloat, color: UIColor) {
        layer.borderWidth = width
        layer.borderColor = color.cgColor
    }
}


extension MBProgressHUD {

    // MARK: - 显示信息

    class func show(text: String?, icon: String?) -> Void {
        let view = UIApplication.shared.keyWindow!

        // 快速显示一个提示信息
        let hud = MBProgressHUD.showAdded(to: view, animated: true)
        //        hud.labelText = text
        hud.detailsLabel.text = text!

        hud.isUserInteractionEnabled = false

        // 设置图片
        hud.customView = UIImageView.init(image: UIImage.init(named: icon!))

        // 再设置模式
        hud.mode = MBProgressHUDMode.customView

        // 隐藏时候从父控件中移除
        hud.removeFromSuperViewOnHide = true

        // 指定距离中心点Y轴的距离(正值向下，负值向上)
        hud.offset.y = CGFloat(view.frame.size.height / 2) - 90;

        // 1秒之后再消失
        hud.hide(animated: true, afterDelay: 1)
    }

    // MARK: - 显示一些信息

    class func showMessage(_ message:String?) {

        self.showMessage(message, toView: nil)
    }

    class func showMessage(_ message: String?, toView: UIView?) {

        var view = toView
        if view == nil {
            view = UIApplication.shared.keyWindow!
        }

        let hud = MBProgressHUD.showAdded(to: view!, animated: true)
        hud.label.text = message!
        // 隐藏时候从父控件中移除
        hud.removeFromSuperViewOnHide = true
        // YES代表需要蒙版效果
        hud.backgroundView.style = MBProgressHUDBackgroundStyle.solidColor
//        hud.backgroundView.color = UIColor.init(white: 0, alpha: 0.2)

        hud.show(animated: true)
    }


    // MARK: - 显示成功或者错误信息

    class func showSuccess(success: String?, toView: UIView?) -> Void {
        show(text: success, icon: "success.png")

    }

    class func showError(error: String?, toView: UIView?) -> Void {
        show(text: error, icon: "")
    }

    class func showSuccess(success: String?) -> Void {
        showSuccess(success: success, toView: nil)
    }

    class func showError(error: String?) -> Void {
        showError(error: error, toView: nil)
    }

    class func hideHUD(view: UIView?) -> Void {
        hide(for: view!, animated: true)
    }

    class func hideHUD() -> Void {
        let view = UIApplication.shared.keyWindow
        hideHUD(view: view)
    }

    class func showCustomHUD(message: String?) ->Void {

        // 把显示的消息放到试图的最顶层keyWindow上面
        let view = UIApplication.shared.keyWindow!
        // 快速显示一个提示信息
        let hud = MBProgressHUD.showAdded(to: view, animated: true)
        // 显示消息内容
        hud.detailsLabel.text = message!
        // 不允许用户操作
        hud.isUserInteractionEnabled = false
        // 再设置模式
        hud.mode = MBProgressHUDMode.customView
        // 隐藏时候从父控件中移除
        hud.removeFromSuperViewOnHide = true
        // 1秒之后再消失
        hud.hide(animated: true, afterDelay: 1)
    }
}


extension String {
 
    /// 根据文本内容计算Label的高度
    ///
    /// - Parameters:
    ///   - font: 字体大小
    ///   - width: 显示宽度
    ///   - maxHeight: 最大显示高度（CGFloat.greatestFiniteMagnitude：最大的有限高度）
    /// - Returns: Label高度
    func getTextLabelHeight(font: UIFont, width: CGFloat, maxHeight: CGFloat) -> CGFloat {
        // 转换为属性字符串，并设置字体大小
        let attributeString  = NSMutableAttributedString.init(string: self)
        attributeString.addAttributes([NSAttributedString.Key.font: font], range: NSMakeRange(0, self.count))
        // 计算高度
        let height = attributeString.boundingRect(with: CGSize(width: width, height: maxHeight), options: NSStringDrawingOptions(rawValue: NSStringDrawingOptions.usesLineFragmentOrigin.rawValue), context: nil).height
        
        return height
    }
    
    /// 设置行间距
    ///
    /// - Parameter lineSpace: 行间距
    /// - Returns: 属性字符串
    func setLineSpaceAndFont(lineSpace: CGFloat, font: UIFont) -> NSMutableAttributedString {
        
        let attributeString = NSMutableAttributedString(string: self)
        let paragraphStye = NSMutableParagraphStyle()
        //调整行间距
        paragraphStye.lineSpacing = lineSpace
        attributeString.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStye, range: NSMakeRange(0, self.count))
        attributeString.addAttributes([NSAttributedString.Key.font: font], range: NSMakeRange(0, self.count))
        
        return attributeString
    }
    
    // string to CGFloat
    func toFloat() -> CGFloat {
        
        var cgFloat: CGFloat = 0
        if let doubleValue = Double(self) {
            cgFloat = CGFloat(doubleValue)
        }
        
        return cgFloat
    }
}

extension NSMutableAttributedString {
    
    func getLabelHeight(width: CGFloat, maxHeight: CGFloat) -> CGFloat {
        // 计算高度
        let height = self.boundingRect(with: CGSize(width: width, height: maxHeight), options: NSStringDrawingOptions(rawValue: NSStringDrawingOptions.usesLineFragmentOrigin.rawValue), context: nil).height
        
        return height
    }
}

